﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Linq;

namespace IEProxySwitch
{
    public partial class MainForm : Form
    {
        [DllImport("wininet.dll", SetLastError = true)]
        private static extern bool InternetSetOption(IntPtr hInternet,
        int dwOption,
        IntPtr lpBuffer,
        int lpdwBufferLength);
        
        const int INTERNET_OPTION_SETTINGS_CHANGED = 39;

        string regKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
       
        public MainForm()
        {
            InitializeComponent();
            checkProxy();
            
        }
        private void btnEnable_Click(object sender, EventArgs e)
        {
            if (checkProxy() == 0) {
                SetRegValue("ProxyEnable", "1");
            } else {
                SetRegValue("ProxyEnable", "0");
            }

            checkProxy();
        }



        private string GetRegValue(string key){
            RegistryKey proxy = Registry.CurrentUser.OpenSubKey(regKey, false);
            var retval= proxy.GetValue(key).ToString();
            proxy.Close();
            return retval;
        }

        private void SetRegValue(string key,String value)
        {
            RegistryKey cUser = Registry.CurrentUser.OpenSubKey(regKey, true);
            if (char.IsDigit(value[0])){
                cUser.SetValue(key, int.Parse(value));        // doesn't work if string is paased for numeric values    
            }
            else {
                cUser.SetValue(key, value);            
            }
            bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);            
        }

        

        private int checkProxy()
        {
            this.lblProxySet.Text = GetRegValue("ProxyServer");
            int enabled = int.Parse(GetRegValue("ProxyEnable")); 
            if (enabled == 1)
            {
                lblStatus.ForeColor = Color.Red;
                lblStatus.Text = "Enabled";
                btnEnable.Text = "Disable Proxy";
                notifyIcon1.Icon = global::IEProxySwitch.Properties.Resources.green;
                imgGreen.Image = global::IEProxySwitch.Properties.Resources.greenimg;
                mnuDisableProxy.Visible = true;
                mnuEnableProxy.Visible = false;
            }
            else
            {
                lblStatus.ForeColor = Color.Green;
                lblStatus.Text = "Disabled";
                btnEnable.Text = "Enable Proxy";
                notifyIcon1.Icon = global::IEProxySwitch.Properties.Resources.red;
                imgGreen.Image = global::IEProxySwitch.Properties.Resources.redimg;
                mnuEnableProxy.Visible = true;
                mnuDisableProxy.Visible = false;
            }
            notifyIcon1.Visible = true;
            return enabled;
        }
        private void btnDisable_Click(object sender, EventArgs e)
        {
            SetRegValue("ProxyEnable", "0");
            checkProxy();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized) {
                Visible = false;
                notifyIcon1.Visible = true;
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            this.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          var lstString= global::IEProxySwitch.Properties.Settings.Default.proxyList + "";
          var lst = lstString.Split('\n');
          cboProxyList.Items.AddRange(lst);

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void mnuEnableProxy_Click(object sender, EventArgs e)
        {
            btnEnable.PerformClick();
        }

        private void mnuDisableProxy_Click(object sender, EventArgs e)
        {
            btnEnable.PerformClick();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            global::IEProxySwitch.Properties.Settings.Default.proxyList = string.Join("\n",cboProxyList.Items.Cast<string>().ToArray());
            global::IEProxySwitch.Properties.Settings.Default.Save();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

            cboProxyList.Items.Clear();
           // cboProxyList.Items.AddRange(textBox1.Lines);

            toolStripComboBox1.Items.Clear();
           // toolStripComboBox1.Items.AddRange(textBox1.Lines);
        }

        private void toolStripComboBox1_Click(object sender, EventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var res = new IEProxySwitch.ProxyList();

            res.SetList(cboProxyList.Items.Cast<string>().ToArray());

            if (res.ShowDialog() == DialogResult.OK)
            {
                cboProxyList.Items.Clear();
                cboProxyList.Items.AddRange(res.GetList());
            }
        }

        private void cmdApply_Click(object sender, EventArgs e)
        {
            if (cboProxyList.SelectedIndex > -1) {
                if (MessageBox.Show("Apply " + cboProxyList.SelectedItem.ToString() + " as Proxy ?","Confirm !!",MessageBoxButtons.YesNo) == DialogResult.Yes) {
                    SetRegValue("ProxyServer", cboProxyList.SelectedItem.ToString());
                }
            }
        }        
     
    }
}